package com.jianzh5.action.admin;

import com.jianzh5.action.BaseAction;
import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.common.ResultJson;
import com.jianzh5.entity.TStatic;
import com.jianzh5.pojo.PCategory;
import com.jianzh5.service.StaticServiceI;
import com.jianzh5.util.InfoCenterHome;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Collections;

/**
 * <p>
 * <code>StaticAction</code>
 * </p>
 * @author jianzhang11
 * @version 1.0
 * @desc
 * @Date: 2016/8/29 17:10
 * @since 1.0
 */

@Controller
@RequestMapping("/cms/static")
public class StaticAction extends BaseAction{

    @Autowired
    private StaticServiceI staticService;


    @RequestMapping(value = "/staticList" ,method = RequestMethod.GET)
    @RequiresPermissions("cms:static:view")
    public String staticList(){
        return InfoCenterHome.SERVLET_ADMIN_ROOT+"cms/staticList";
    }

    @RequestMapping(value = "/staticList",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("cms:static:view")
    public DataTables staticList(DataPage datapage, HttpServletRequest request){
        String sindex = request.getParameter("order[0][column]");
        String order = request.getParameter("order[0][dir]");
        String sort = request.getParameter("columns["+sindex+"][data]");
        DataTables dt = staticService.dataTables(order,sort,datapage);
        return dt;
    }

    @RequiresPermissions("cms:static:edit")
    @RequestMapping("/edit/{id}")
    public String editStaticPage(@PathVariable int id,Model model){
        TStatic tStatic = staticService.getStatic(id);
        model.addAttribute("tStatic", tStatic);
        return InfoCenterHome.SERVLET_ADMIN_ROOT+"/cms/staticEdit";
    }


    @RequiresPermissions("cms:static:edit")
    @RequestMapping("/staticEdit")
    @ResponseBody
    public ResultJson staticEdit(TStatic tStatic){
        ResultJson json = new ResultJson();
        try{
            staticService.editStatic(tStatic);
            json.setSuccess(true);
            json.setMsg("页面内容编辑成功...");
        }catch (Exception e) {
            json.setMsg("页面内容编辑异常...");
        }
        return json;
    }


}
